<template>
  <div class="article-container">
    <!-- 文章内容区 -->
    <div class="article-content">
      <!-- 标题 -->
      <div ref="title" class="article-title"></div>
      
      <!-- 正文 -->
      <div ref="content" class="article-body"></div>
    </div>

    <!-- 评论区 -->
    <div ref="comment" class="comment-section">
      <div class="comment-header">
        <h3>评论 ({{ commentList.length }})</h3>
      </div>

      <!-- 评论输入框 -->
      <div class="comment-input-container">
        <Input 
          v-model="commentText" 
          type="textarea" 
          :rows="2"
          placeholder="写下你的评论..."
          @on-enter="sendComment"
        />
        <Button 
          type="primary" 
          :disabled="!commentText"
          @click="sendComment"
          class="comment-submit"
        >
          发布评论
        </Button>
      </div>

      <!-- 评论列表 -->
      <div class="comment-list">
        <Card v-for="item in commentList" 
              :key="item.id"
              class="comment-item">
          <div class="comment-user">
            <div class="avatar-container">
              <img :src="item.avatar" :alt="item.nickname" class="user-avatar">
            </div>
            <span class="comment-username">{{ item.nickname }}</span>
          </div>
          <div class="comment-content">
            {{ item.comment }}
          </div>
          <div class="comment-time">
            {{ formatTime(item.createTime) }}
          </div>
          <Button type="error" @click="deleteComment(item)" style="position: absolute; right: 10px; top: 10px;" v-if="isOwner || currentUser.id === item.userId">删除</Button>
        </Card>
        
        <!-- 无评论提示 -->
        <div v-if="commentList.length === 0" class="no-comments">
          <Icon type="ios-chatbubbles-outline" size="30"/>
          <p>暂无评论，快来抢沙发吧！</p>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import {getByFriendUpdatesId, getFriendUpdatesVOById, saveComment, getFriendUpdatesVOByUserIdAndId, deleteComment} from "@/api/api"
import dayjs from 'dayjs'

export default {
  name: "FriendUpdateInfo",
  data() {
    return {
      commentText: null,
      commentList: [],
      isOwner: false,
      currentUser: JSON.parse(sessionStorage.getItem("user")),
    }
  },
  created() {
    getFriendUpdatesVOByUserIdAndId(this.currentUser.id, this.$route.query.friendUpdateId).then(res => {
      if (res.body.code === 1) {
        this.isOwner = res.body.data != null
      }
    })
  },

  methods: {
    sendComment() {
      if (!this.commentText?.trim()) {
        this.$Message.warning('评论内容不能为空')
        return
      }

      const user = JSON.parse(sessionStorage.getItem("user"))
      if (!user) {
        this.$Message.warning('请先登录')
        return
      }

      const data = {
        userId: user.id,
        friendUpdatesId: this.$route.query.friendUpdateId,
        comment: this.commentText.trim()
      }

      saveComment(data).then(res => {
        if (res.body.code === 1) {
          this.$Message.success("评论成功")
          this.loadComment()
          this.commentText = ""
        } else {
          this.$Message.error(res.body.msg || "评论失败")
        }
      })
    },

    loadComment() {
      getByFriendUpdatesId(this.$route.query.friendUpdateId).then(res => {
        if (res.body.code === 1) {
          this.commentList = res.body.data
        } else {
          this.$Message.error(res.body.msg)
        }
      })
    },

    formatTime(time) {
      return dayjs(time).format('YYYY-MM-DD HH:mm')
    },

    deleteComment(item) {
      deleteComment(item.id).then(res => {
        if (res.body.code === 1) {
          this.$Message.success("删除成功")
          this.loadComment()
        } else {
          this.$Message.error(res.body.msg || "删除失败")
        }
      })
    }
  },
  mounted() {
    getFriendUpdatesVOById(this.$route.query.friendUpdateId).then(res => {
      if (res.body.code === 1) {
        this.$refs.title.innerHTML = res.body.data.styleTitle
        this.$refs.content.innerHTML = res.body.data.styleContent
      } else {
        this.$Message.error(res.body.msg)
      }
    })

    this.loadComment()
  }
}
</script>

<style scoped>
.article-container {
  width: 75vw;
  margin: 20px auto;
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.article-content {
  background: white;
  border-radius: 10px;
  padding: 30px;
}

.article-title {
  margin-bottom: 20px;
}

.article-body {
  color: #333;
  line-height: 1.8;
}

.comment-section {
  background: white;
  border-radius: 10px;
  padding: 20px;
}

.comment-header {
  margin-bottom: 20px;
  padding-bottom: 15px;
  border-bottom: 1px solid #f0f0f0;
}

.comment-header h3 {
  font-size: 18px;
  color: #333;
}

.comment-input-container {
  margin-bottom: 20px;
}

.comment-submit {
  margin-top: 10px;
  float: right;
}

.comment-list {
  margin-top: 40px;
}

.comment-item {
  margin-bottom: 15px;
}

.avatar-container {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  overflow: hidden;
}

.user-avatar {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.comment-user {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 8px;
}

.comment-username {
  font-weight: 500;
  color: #333;
}

.comment-content {
  color: #666;
  line-height: 1.6;
  margin: 8px 0;
}

.comment-time {
  font-size: 12px;
  color: #999;
}

.no-comments {
  text-align: center;
  padding: 40px 0;
  color: #999;
}

.no-comments p {
  margin-top: 10px;
}

/* 确保文章内容中的图片不会溢出 */
:deep(img) {
  max-width: 100%;
  height: auto;
}
</style>